package com.yksj.healthtalk.ui.friend;

import java.util.ArrayList;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.adapter.SearchAdapter;
import com.yksj.healthtalk.adapter.SearchAdapter.onClickFriendAttentionListener;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.MessagePushService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.FriendUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 黑名单
 * 
 * @author root
 * 
 */
public class FriendBlackListActivity extends BaseFragmentActivity implements
		OnClickListener, OnItemClickListener, OnRefreshListener2<ListView>, onClickFriendAttentionListener {

	private PullToRefreshListView mPullToRefreshListView;
	private TextView mTitleName;
	private Button mTitleBack;
	private ListView mListView;
	private SearchAdapter adapter;
	private ArrayList<CustomerInfoEntity> infoEntities;
	private int pagenum = 20;
	private int pagesize = 1;
	CustomerInfoEntity cacheCustomerInfoEntity;//改变之后
	private BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if(action.equals(MessagePushService.ACTION_COLLECT_FRIEND)){
				String result = intent.getStringExtra("result");
				if (result.equals("0")) {
					ToastUtil.showShort(FriendBlackListActivity.this, R.string.groupNewFail);
				} else if (result.equals("-1")) {
					ToastUtil.showShort(FriendBlackListActivity.this,R.string.against__blacklist_operations);
				} else {
					infoEntities.remove(cacheCustomerInfoEntity);
					adapter.notifyDataSetChanged();
					FriendHttpUtil.requestAttentionTofriendsResult(FriendBlackListActivity.this, cacheCustomerInfoEntity, mAppData);
				}
			}
		}
	};
	private AppData mAppData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend_exact_search);
		mAppData = HTalkApplication.getAppData();
		initWidget();
		initData();
		
		mPullToRefreshListView.setRefreshing();
		getBlackList(String.valueOf(pagenum),String.valueOf(pagesize));
	}

	private void getBlackList( String pagenum,
			final String pagesize) {
		HttpRestClient.doHttpFindMyBlacks(SmartFoxClient.getLoginUserId(), pagenum, pagesize,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, String content) {
						ArrayList<CustomerInfoEntity> entities = FriendHttpUtil.jsonAnalysisFriendEntity(content, true);
						if (pagesize.equals("1")) {
							infoEntities.clear();
						}
						infoEntities.addAll(entities);
						adapter.notifyDataSetChanged();
						super.onSuccess(statusCode, content);
					}

					@Override
					public void onFinish() {
						mPullToRefreshListView.onRefreshComplete();
						super.onFinish();
					}
				});
	}

	private void initData() {
		mTitleName.setText("黑名单");
		infoEntities = new ArrayList<CustomerInfoEntity>();
		adapter = new SearchAdapter<CustomerInfoEntity>(this, infoEntities);
		adapter.setType(0);
		adapter.setonClickFriendAttentionListener(this);
		mListView.setOnItemClickListener(this);
		mListView.setAdapter(adapter);
	}


	private void initWidget() {
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.hall);
		mTitleBack = (Button) findViewById(R.id.title_back);
		mTitleName = (TextView) findViewById(R.id.title_lable);
		mListView = mPullToRefreshListView.getRefreshableView();
		
		mListView.setFooterDividersEnabled(false);
		mListView.addFooterView(getLayoutInflater().inflate(R.layout.line_layout, null));
		mListView.setEmptyView(getLayoutInflater().inflate(R.layout.listview_empty_view, null));
		
		mTitleBack.setOnClickListener(this);
		mPullToRefreshListView.setOnRefreshListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getTag() != null && v.getTag() instanceof Integer) {
			int position = (Integer) v.getTag();
			Intent intent;
			if (!FriendUtil.isDoctor(infoEntities.get(position))) {
				intent = new Intent();
				intent.setClass(this, FriendInformUI.class);
				intent.putExtra("id", infoEntities.get(position).getId());
				startActivityForResult(intent, 1000);
			} else {
				 intent = new Intent(this, DoctorMainActivity.class);
				intent.putExtra("id", infoEntities.get(position).getId());
				startActivityForResult(intent, 1000);
			}
		}
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		FriendHttpUtil.onItemClick(this, infoEntities.get(position - 1));
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		pagenum = 20;
		pagesize = 1;
		getBlackList(String.valueOf(pagenum),String.valueOf(pagesize));
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		getBlackList(String.valueOf(pagenum),String.valueOf(++pagesize));
	}

	@Override
	public void onClickFriendAttention(int attentioned,CustomerInfoEntity entity, int positon) {
		cacheCustomerInfoEntity=FriendHttpUtil.requestAttentionTofriends(this, true, entity);
	}
	

	@Override
	public void onStart() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(MessagePushService.ACTION_COLLECT_FRIEND);
		registerReceiver(receiver, filter);
		super.onStart();
	}
	
	@Override
	public void onStop() {
	unregisterReceiver(receiver);
		super.onStop();
	}

}
